package com.jinke.api.modules.app.util;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jinke.api.modules.third.ezeeship.model.request.EstimateRateRequest;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import static com.jinke.api.modules.third.dqpost.util.DqpostlUtil.NUM_PLACES;
import static com.jinke.api.modules.third.dqpost.util.DqpostlUtil.ROUND_TYPE;

@Data
@Component
public class MathUtil {

    /**
     * 设置保留两位小数并向上取整
     * @param input
     * @return
     */
    public static BigDecimal formatBigDecimal(BigDecimal input) {
        return input.setScale(2, RoundingMode.CEILING);
    }

    // oz->lb
    public static BigDecimal oz2lb(BigDecimal input) {
        // 设置保留两位小数并向上取整
        return input.divide(BigDecimal.valueOf(16), NUM_PLACES, ROUND_TYPE);
    }

    public static BigDecimal g2kg(BigDecimal input) {
        // 设置保留两位小数并向上取整
        return input.divide(BigDecimal.valueOf(1000), NUM_PLACES, ROUND_TYPE);
    }

    public static BigDecimal kg2lb(BigDecimal input) {
        // 设置保留两位小数并向上取整
        return input.divide(BigDecimal.valueOf(0.45), NUM_PLACES, ROUND_TYPE);
    }
    public static BigDecimal kg2oz(BigDecimal input) {
        // 设置保留两位小数并向上取整
        return lb2oz(kg2lb(input));
    }
    public static BigDecimal lb2kg(BigDecimal input) {
        // 设置保留两位小数并向上取整
        return input.multiply(BigDecimal.valueOf(0.45)).setScale(2, RoundingMode.CEILING);
    }
    public static BigDecimal lb2oz(BigDecimal input) {
        // 设置保留两位小数并向上取整
        return input.multiply(BigDecimal.valueOf(16));
    }
    public static BigDecimal cm2in(BigDecimal input) {
        // 设置保留两位小数并向上取整
        return input.divide(BigDecimal.valueOf(2.54), NUM_PLACES, ROUND_TYPE);
    }
    public static BigDecimal in2cm(BigDecimal input) {
        // 设置保留两位小数并向上取整
        return input.multiply(BigDecimal.valueOf(2.54)).setScale(2, RoundingMode.CEILING);
    }

    public static void fmtWeight2(EstimateRateRequest request) {
        List<EstimateRateRequest.Parcel> parcels = request.getParcels();
        parcels.forEach(x -> {
            String weight = x.getWeight();
            String weight2 = x.getWeight2();
            if (StringUtils.isNotBlank(weight2)) {
                BigDecimal w2 = BigDecimal.valueOf(Double.parseDouble(weight2));
                if ("lb".equals(x.getMassUnit())) { // oz->lb
                    w2 = MathUtil.oz2lb(w2);
                } else {// g->kg
                    w2 = MathUtil.g2kg(w2);

                }
                weight = BigDecimal.valueOf(Double.parseDouble(weight)).add(w2).toString();

            }

            // 保留2位小数
            x.setWeight( formatBigDecimal(BigDecimal.valueOf(Double.parseDouble(weight))).toString());
            x.setHeight(formatBigDecimal(BigDecimal.valueOf(Double.parseDouble(x.getHeight()))).toString());
            x.setWidth(formatBigDecimal(BigDecimal.valueOf(Double.parseDouble(x.getWidth()))).toString());
            x.setLength(formatBigDecimal(BigDecimal.valueOf(Double.parseDouble(x.getLength()))).toString());
        });
    }

}
